﻿#if PocketPC && !DesignTime
using System;
using System.Reflection;
using System.Collections;
using System.Text;

namespace GeoFramework.Licensing
{
	/// <summary>Indicates the operational environment allowed for a license.</summary>
	/// <remarks>
	/// This enumeration is used by the <strong>LicenseContext</strong> class to indicate
	/// the operational environment valid for a license.
	/// </remarks>
	public enum LicenseUsageMode
	{
		/// <summary>
		/// The license is valid for use within the Visual Studio.NET Windows Forms
		/// designer.
		/// </summary>
		Designtime,
		/// <summary>The license is valid for use in a production or release environment.</summary>
		Runtime
	}

	/// <summary>Provides general license validation management services for a class.</summary>
	/// <remarks>
	/// 	<para>This class is used to perform generic license validation and registration
	///     services. Two methods are provided:
	///     <see cref="Validate(System.Type,System.Object)">Validate</see> and
	///     <see cref="RegisterLicense">RegisterLicense</see>. The <strong>Validate</strong>
	///     method performs license validation and attempts to return a License object.
	///     <strong>RegisterLicense</strong> is used by developers to make license keys
	///     available to the Validate method.</para>
	/// 	<para>This class exists only for .NET Compact Framework 1.0, which excluded
	///     licensing classes by default.</para>
	/// </remarks>
    public 
#if Framework20
	static
#else
		sealed
#endif
		class LicenseManager
    {
        private static LicenseContext _CurrentContext;
        private static LicenseUsageMode _UsageMode;

        static LicenseManager()
        {
            // Configure the context and usage mode
#if DesignTime
            _UsageMode = LicenseUsageMode.Designtime);
#else
#if PocketPC
            // If the entry assembly is null, we may be on the desktop still	        
            if (GetEntryAssembly() == null)
                _UsageMode = LicenseUsageMode.Designtime;
            else
                _UsageMode = LicenseUsageMode.Runtime;
#else
			_UsageMode = LicenseUsaeMode.Runtime;
#endif
#endif
            _CurrentContext = new LicenseContext(_UsageMode);
        }

#if PocketPC
        /// <summary>
        /// Gets the process executable.
        /// </summary>
        /// <returns>The <see cref="Assembly"/> that is the process executable.</returns>
        internal static Assembly GetEntryAssembly()
        {
            StringBuilder sb = null;
            IntPtr hModule = NativeMethods.GetModuleHandle(IntPtr.Zero);
            if (IntPtr.Zero != hModule)
            {
                sb = new StringBuilder(255);
                if (NativeMethods.GetModuleFileName(hModule, sb, sb.Capacity) == 0)
                {
                    sb = null;
                }
            }

            // If the name contains "devenv.exe"?
            string AssemblyPath = sb.ToString();

            try
            {
                // Load the assembly from the given path
                return Assembly.LoadFrom(sb.ToString());
            }
#if !PocketPC || Framework20
            catch (BadImageFormatException)
            {
                // The load failed.  This could be DEVENV.EXE or another unmanaged executable.
                return null;
            }
            catch
            {
                throw;
            }
#else
			catch 
			{
				// The load failed.  This could be DEVENV.EXE or another unmanaged executable.
				return null;
			}
#endif
        }


#endif

        public static LicenseContext CurrentContext
        {
            get
            {
                return _CurrentContext;
            }
        }

        public static LicenseUsageMode UsageMode
        {
            get
            {
                return _UsageMode;
            }
        }
    }
}
#endif